package com.jse;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Zips {

	public static void listerZip(String nomFichier) {
	    try(var zipFile = new ZipFile(nomFichier)){
	      ;
	      var entries = zipFile.entries();
	      while (entries.hasMoreElements()) {
	        ZipEntry entry = (ZipEntry) entries.nextElement();
	        String name = entry.getName();
	        System.out.println(name);
	      }
	      zipFile.close();
	    } catch (IOException e) {
	      e.printStackTrace();
	    }
	  }
	
	//压缩
	public static void compresser(String nomArchive, String nomFichier) {
		var fichier = new File(nomFichier);
	    try(
	    		ZipOutputStream zip = new ZipOutputStream(
		          new FileOutputStream(nomArchive));
	    	var fis = new FileInputStream(fichier);){
	      
	      zip.setMethod(ZipOutputStream.DEFLATED);
	      zip.setLevel(Deflater.BEST_COMPRESSION);

	      // lecture du fichier
	      
	      
	      byte[] bytes = new byte[fis.available()];
	      fis.read(bytes);

	      // ajout d'une nouvelle entrée dans l'archive contenant le fichier
	      ZipEntry entry = new ZipEntry(nomFichier);
	      entry.setTime(fichier.lastModified());
	      zip.putNextEntry(entry);
	      zip.write(bytes);

	      // fermeture des flux
	      zip.closeEntry();
	    } catch (FileNotFoundException fnfe) {
	      fnfe.printStackTrace();
	    } catch (IOException ioe) {
	      ioe.printStackTrace();
	    }
	  }

	//解压缩
	public static void decompresser(String nomArchive, String chemin) {
	    try {

	      ZipFile zipFile = new ZipFile(nomArchive);
	      var entries = zipFile.entries();
	      ZipEntry entry = null;
	      File fichier = null;
	      File sousRep = null;

	      while (entries.hasMoreElements()) {
	        entry = (ZipEntry) entries.nextElement();

	        if (!entry.isDirectory()) {
	          System.out.println("Extraction du fichier " + entry.getName());
	          fichier = new File(chemin + File.separatorChar + entry.getName());
	          sousRep = fichier.getParentFile();

	          if (sousRep != null) {
	            if (!sousRep.exists()) {
	              sousRep.mkdirs();
	            }
	          }

	          int i = 0;
	          byte[] bytes = new byte[1024];
	          BufferedOutputStream out = new BufferedOutputStream(
	              new FileOutputStream(fichier));
	          BufferedInputStream in = new BufferedInputStream(zipFile
	              .getInputStream(entry));
	          while ((i = in.read(bytes)) != -1)
	            out.write(
	                bytes,
	                0,
	                i);

	          in.close();
	          out.flush();
	          out.close();
	        }
	      }
	      zipFile.close();
	    } catch (FileNotFoundException fnfe) {
	      fnfe.printStackTrace();
	    } catch (IOException ioe) {
	      ioe.printStackTrace();
	    }
	  }
}
